<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改权限</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/default/layer.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/layui.css" rel="stylesheet">
    <link href="css/plugins/jsTree/style.min.css" rel="stylesheet">

    <style>
        #nestable,
        #jstree2 {
            width: 30%;
            float: left;
            height: 100%;
            border-right: 1px solid #eee;
            padding: 10px;
            background: #fff;
        }

        .pull-right i {
            margin: 0 5px;
        }

        #jstree1 {
            padding: 10px;
        }

        .box-right {
            width: 70%;
            float: right;
            padding: 10px;
            height: 100%;
            background: #fff;
        }

        #uploadImg {
            position: absolute;
            top: 50%;
            left: 48%;
            z-index: 999;
            visibility: visible;
        }

        #bg {
            background: #eee;
            position: absolute;
            z-index: 998;
            display: none;
            width: 100%;
            height: 100%;
            filter: alpha(opacity=50);
            -moz-opacity: 0.5;
            -khtml-opacity: 0.5;
            opacity: 0.5;
        }
        .bg-inp{
            background-color: antiquewhite
        }
    </style>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <form class="form-horizontal m-t" id="commentForm">


                                    <div class="layui-form-item form_item_align_left">
                                        <div class="layui-inline">
                                            <label class="layui-form-label">角色名称</label>
                                            <div class="layui-input-inline">
                                                <input id="roleName" placeholder="角色code值，输入英文" readonly="readonly" name="roleName" autocomplete="off" class="layui-input bg-inp" aria-required="true">
                                            </div>
                                        </div>
                                    </div>



                                    <div class="col-sm-10" style="padding-left: 100px;">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label" style="padding-top:10px;">权限：</label>
                                            <div class="col-sm-7">
                                                <!-- <input type="text" class="form-control" name="password" id="departmentName"> -->
                                                <div id="jstree1">

                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-xs-3 col-xs-offset-3">
                                    <button class="btn btn-success" type="submit">确定</button>
                                </div>
                                <div class="col-xs-3">
                                    <button class="btn btn-danger" type="submit">取消</button>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 全局js -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- 自定义js -->
    <script src="js/content.js"></script>
    <script src="js/plugins/layer/layer.js"></script>
    <script src="js/layui.js"></script>
    <script src="js/plugins/validate/jquery.validate.min.js"></script>
    <script src="js/plugins/validate/messages_zh.min.js"></script>
    <script src="js/plugins/jsTree/jstree.min.js"></script>
    <script src="js/work/common.js"></script>
    <script>
        layui.use(['form'], function(){});
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

        var selectAppkey;
        var selectRoleId;
        var selectRoleName;
        $(document).ready(function () {
            selectRoleName = getStorage("roleName");
            selectRoleId = getStorage("roleId");
            selectAppkey = getStorage("appKey");
            initData();

            // 取消
            $(".btn-danger").on("click", function () {
                parent.layer.close(index);
            });


            var flag=0;
            // 确认操作
            $(".btn-success").on("click", function () {
                // 获取选中的id集合
                var idList = ($('#jstree1').jstree('get_selected')).toString();
                console.log(idList);
                updateRolePermission(idList);
            });
        });

        function initData() {
           $("#roleName").val(selectRoleName);
            ajax({
                url: "resource/resourceFindByRoleId",
                type: "GET",
                dataType: 'jsonp',
                jsonp: "callback",
                async: true,
                data: {
                    appKey:selectAppkey,
                    roleId:selectRoleId,
                },
                timeout: 8000,
                error: function (xhr, type) {
                    layer.msg("服务器异常，请稍后重试！", {time:2000})
                },
                successx: function (data) {
                    if (data.success == true) {
                        console.log(data);
                        initResources(data.data)
                    } else {
                        layer.msg(data.message,{time:1000})
                    }
                },
            });
        }

        function initResources(data) {
            // data = twoJson;
            $('#jstree1').jstree("destroy");
            $('#jstree1').jstree({
                'core': {
                    'check_callback': true,
                    'data': data
                },
                "checkbox": {
                    "keep_selected_style": false    // 是否默认选中
                },
                'plugins': ['checkbox', 'state'],
                'state': {
                  "opened": true
                },
                "checkbox": {
                    "three_state": false
                }
            });
            // setTimeout(function () {
                $('#jstree1').on('changed.jstree', function (e, data) {
                    // 判断选中状态
                    console.log(data);
                    if (data.node.state.selected) {
                        if (data.node.children_d.length > 0) {
                            for (var i = 0; i < data.node.children_d.length; i++) {
                                $(e.target).jstree('check_node', data.node.children_d[i]);
                            }
                        }
                    }
                });
            // }, 50)
        }


        function updateRolePermission(roleIds) {
            ajaxPost({
                url: "menu/updateRoleMenuInfo",
                async: true,
                data: {
                    roleId:selectRoleId,
                    lists:roleIds,
                },
                timeout: 12000,
                error: function (xhr, type) {
                    layer.msg("服务器异常，请稍后重试！", {time:2000})
                },
                successx: function (data) {
                    if (data.success) {
                        parent.layer.msg(data.message);
                        setTimeout(function () {
                            parent.window.location.href = "role.html";
                            parent.layer.close(index);
                        }, 1800)
                    } else {
                        parent.layer.msg(data.message)
                    }
                },
            });
        }



        /*var twoJson = {
            "success": true,
            "code": 200,
            "message": "成功",
            "data": {
                "tblUpmsRoleInfoDat": {
                    "role_id": 15,
                    "parent_id": 12,
                    "parent_path": "0/1/12/",
                    "role_name": "wux-user",
                    "priority": 0,
                    "is_delete": 0,
                    "description": "",
                    "create_time": 1520956571000,
                    "update_time": 1520956571000,
                    "update_people": "",
                    "id": 15
                },
                "queryByOrganization": [
                    {
                        "id": 1,
                        "text": "资源",
                        "resource_id": 1,
                        "parent_id": 0,
                        "parent_path": "0/",
                        "permissions_code": "",
                        "resource_name": "资源",
                        "resource_type": "menu",
                        "resource_request_uri": "",
                        "resource_image_uri": "",
                        "priority": 0,
                        "is_delete": 0,
                        "description": "",
                        "create_time": 1506637769000,
                        "update_time": 1506637769000,
                        "isPower": "0",
                        "children": [
                            {
                                "id": 116,
                                "text": "平台概况",
                                "resource_id": 116,
                                "parent_id": 1,
                                "parent_path": "0/1/",
                                "permissions_code": "basic",
                                "resource_name": "平台概况",
                                "resource_type": "menu",
                                "resource_request_uri": "basic.html",
                                "resource_image_uri": "fa-bar-chart",
                                "priority": 0,
                                "is_delete": 0,
                                "description": "basic",
                                "create_time": 1526586603000,
                                "update_time": 1527004801000,
                                "isPower": "0",
                                "children": null,
                                "state": {
                                    "selected":true,
                                    "opened":true,
                                    "disabled":false
                                }
                            },
                            {
                                "id": 2,
                                "text": "机场管理",
                                "resource_id": 2,
                                "parent_id": 1,
                                "parent_path": "0/1/",
                                "permissions_code": "",
                                "resource_name": "机场管理",
                                "resource_type": "menu",
                                "resource_request_uri": "",
                                "resource_image_uri": "fa-plane",
                                "priority": 1,
                                "is_delete": 0,
                                "description": "",
                                "create_time": 1506637769000,
                                "update_time": 1527005041000,
                                "isPower": "0",
                                "children": [
                                    {
                                        "id": 44,
                                        "text": "贵宾厅",
                                        "resource_id": 44,
                                        "parent_id": 2,
                                        "parent_path": "0/1/2/",
                                        "permissions_code": "vipRoom",
                                        "resource_name": "贵宾厅",
                                        "resource_type": "menu",
                                        "resource_request_uri": "vipRoom.html",
                                        "resource_image_uri": "fa-vimeo",
                                        "priority": 5,
                                        "is_delete": 0,
                                        "description": "",
                                        "create_time": 1506637769000,
                                        "update_time": 1527015205000,
                                        "isPower": "0",
                                        "children": null,
                                        "state": {
                                            "selected":true,
                                            "opened":true,
                                            "disabled":false
                                        }
                                    },
                                    {
                                        "id": 78,
                                        "text": "商品推荐",
                                        "resource_id": 78,
                                        "parent_id": 2,
                                        "parent_path": "0/1/2/",
                                        "permissions_code": "",
                                        "resource_name": "商品推荐",
                                        "resource_type": "menu",
                                        "resource_request_uri": "RecommendGoods.html",
                                        "resource_image_uri": "fa-thumbs-up",
                                        "priority": 6,
                                        "is_delete": 0,
                                        "description": "",
                                        "create_time": 1513035395000,
                                        "update_time": 1527015212000,
                                        "isPower": "0",
                                        "children": null,
                                        "state": {
                                            "selected":true,
                                            "opened":true,
                                            "disabled":false
                                        }
                                    },
                                    {
                                        "id": 58,
                                        "text": "头条管理",
                                        "resource_id": 58,
                                        "parent_id": 2,
                                        "parent_path": "0/1/2/",
                                        "permissions_code": "cfgCappHome",
                                        "resource_name": "头条管理",
                                        "resource_type": "menu",
                                        "resource_request_uri": "news.html",
                                        "resource_image_uri": "fa-newspaper-o",
                                        "priority": 7,
                                        "is_delete": 0,
                                        "description": "",
                                        "create_time": 1512171418000,
                                        "update_time": 1527015218000,
                                        "isPower": "0",
                                        "children": null,
                                        "state": {
                                            "selected":false,
                                            "opened":true,
                                            "disabled":false
                                        }
                                    }
                                ],
                                "state": {
                                    "selected":false,
                                    "opened":true,
                                    "disabled":false
                                }
                            }
                        ],
                        "state": {
                            "selected":true,
                            "opened":true,
                            "disabled":false
                        }
                    }
                ]
            },
            "totalCount": 0
        }*/
    </script>
</body>
</html>
